﻿using BgWorks.Common;
using BgWorks.Models;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace BgWorks.Works
{
    class RequestHeader
    {
        public string Name { get; set; }
        public string Value { get; set; }
    }
    public class APIWork: IWork
    {

        /// <summary>
        /// 支持多种http请求 delete、put、post、patch
        /// </summary>
        /// <param name="methodname"></param>
        /// <returns></returns>
        private static Method GetHttpMethod(string methodname)
        {

            var method = Method.Post;
            if (!string.IsNullOrEmpty(methodname))
            {
                switch (methodname.ToUpper())
                {
                    case "PUT":
                        method = Method.Put;
                        break;
                    case "DELETE":
                        method = Method.Delete;
                        break;
                    case "PATCH":
                        method = Method.Patch;
                        break;
                    case "OPTIONS":
                        method = Method.Options;
                        break;
                    case "COPY":
                        method = Method.Copy;
                        break;
                    case "GET":
                        method = Method.Get;
                        break;
                    default:
                        break;
                }
            }

            return method;
        }

        public void WorkThing(JobModel jobModel)
        {
            JobData model = jobModel.Data;
            try
            {
                var client = new RestClient(model.url);
                var method = GetHttpMethod(model.methodname);
                var request = new RestRequest(model.resource, method)
                {
                    Timeout = model.timeout
                };
                request.AddParameter("auth", "jhecis", ParameterType.HttpHeader);
                if (!string.IsNullOrEmpty(model.headers))
                {
                    var listHeader = JsonConvert.DeserializeObject<List<RequestHeader>>(model.headers);
                    listHeader?.ForEach(a => { request.AddHeader(a.Name, a.Value); });
                }
                request.AddJsonBody(model.value);
                CancellationTokenSource cancelToken = new CancellationTokenSource();
                cancelToken.CancelAfter(model.timeout);
                var response = client.Execute(request, cancelToken.Token);
                if (response.ErrorException != null)
                {
                    LogHelper.GetLog().Error("ApiWork返回数据异常" + response.ErrorMessage);
                }          
            }
            catch (Exception ex)
            {
                LogHelper.GetLog().Error("执行ApiWork异常"+ex);
                throw new Exception("执行ApiWork异常:" + ex);
            }
        }
    }
}
